package com.wlw.swipeback;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class MainActivity extends SwipeBackActivity implements View.OnClickListener {
    private int[] mBgColors;
    private static int mBgIndex = 0;
    private String mKeyTrackingMode;
    private RadioGroup mTrackingModeGroup;
    private SwipeBackLayout mSwipeBackLayout;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        changeActionBarColor();
        mKeyTrackingMode = getString(R.string.key_tracking_mode);

    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.finish).setOnClickListener(this);
        mTrackingModeGroup = (RadioGroup) findViewById(R.id.tracking_mode);
        mSwipeBackLayout = getSwipeBackLayout();
        mTrackingModeGroup.setOnCheckedChangeListener((group, checkId) -> {
            int edgeFlag = SwipeBackLayout.EDGE_ALL;
            switch (checkId) {
                case R.id.mode_left:
                    edgeFlag = SwipeBackLayout.EDGE_LEFT;
                    break;
                case R.id.mode_right:
                    edgeFlag = SwipeBackLayout.EDGE_RIGHT;
                    break;
                case R.id.mode_bottom:
                    edgeFlag = SwipeBackLayout.EDGE_BOTTOM;
                    break;
                case R.id.mode_all:
                    edgeFlag = SwipeBackLayout.EDGE_ALL;
                    break;
            }
            mSwipeBackLayout.setEdgeTrackingEnabled(edgeFlag);
        });
    }


    private void changeActionBarColor() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setBackgroundDrawable(new ColorDrawable(getColors()[mBgIndex]));
        mBgIndex++;
        if (mBgIndex >= getColors().length)
            mBgIndex = 0;
    }

    private int[] getColors() {
        if (mBgColors == null) {
            Resources resources = getResources();
            mBgColors = new int[]{resources.getColor(R.color.androidColorA)
                    , resources.getColor(R.color.androidColorB)
                    , resources.getColor(R.color.androidColorC)
                    , resources.getColor(R.color.androidColorD)
                    , resources.getColor(R.color.androidColorE)};
        }
        return mBgColors;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_github:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://github.com/Issacw0ng/SwipeBackLayout"));
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                startActivity(new Intent(MainActivity.this, MainActivity.class));
                break;
            case R.id.finish:
                scrollToFinishActivity();
                break;
        }
    }
}
